package com.trust.pams.service.redis.sub;

import com.alibaba.fastjson.JSON;
import com.chasonx.common.config.RedisTemplateUtils;
import com.chasonx.common.util.StringKit;
import com.trust.pams.dao.service.EsPamsSvnDao;
import com.trust.pams.dao.service.PamsSvnDao;
import com.trust.pams.model.Io.SvnGetFileTypeEnum;
import com.trust.pams.model.Tree.Tree;
import com.trust.pams.model.es.SvnEsSearch;
import com.trust.pams.service.redis.config.PamsQuoteCaptcha;
import com.trust.pams.service.redis.config.PubSubConfig;
import com.trust.pams.util.DataUtil;
import com.trust.pams.util.SvnOption;
import com.trust.pams.util.SvnUtil;
import com.trust.pams.util.TreeUtils;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;

import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.io.SVNRepository;

import java.util.*;

@Component
public class SubService extends MessageListenerAdapter {

    private static final Logger log = LoggerFactory.getLogger(SubService.class);
    private static final String tag = "SubService ==> ";

    @Autowired
    private RedisTemplateUtils redisTemplateUtils;

    @Autowired
    private PamsSvnDao pamsSvnDao;

    @Autowired
    private EsPamsSvnDao esPamsSvnDao;

    @SneakyThrows
    public void onMessage(Message message, byte[] pattern) {
        log.info("{}subScribe redis topic: {} message.", tag, PubSubConfig.CAPTCHA_TOPIC);

        byte[] body = message.getBody();
        byte[] channel = message.getChannel();
        String topicMessage = (String) redisTemplateUtils.getRedisTemplate().getValueSerializer().deserialize(body);
        log.info("{}current topicMessage : {}", tag, topicMessage);

        String topic = redisTemplateUtils.getRedisTemplate().getStringSerializer().deserialize(channel);
        log.info("{}current topic : {}", tag, topic);

        if (!PubSubConfig.CAPTCHA_TOPIC.equals(topic)) {
            return;
        }

        if (StringKit.isBlank(topicMessage)) {
            return;
        }
        Object captchaObj = redisTemplateUtils.getRedisTemplate().opsForSet().members(PubSubConfig.CAPTCHA_MAIL_QUEEN);
        redisTemplateUtils.getRedisTemplate().delete(PubSubConfig.CAPTCHA_MAIL_QUEEN);
        if(null != captchaObj){
            Set<PamsQuoteCaptcha> captchas = (Set<PamsQuoteCaptcha>) captchaObj;
            List<PamsQuoteCaptcha> pamsQuoteCaptchaList= new ArrayList<>();
            pamsQuoteCaptchaList.addAll(captchas);
            for(PamsQuoteCaptcha pamsQuoteCaptcha:pamsQuoteCaptchaList){
                String dirPath = "Project";
                SVNRepository svnRepository = SvnUtil.getSVNRepository();
                //获取路径下的所有文件夹或文件
                try {
                    List<Tree> svnList = SvnOption.getSvnFileBaseInfoDto(svnRepository, dirPath, pamsQuoteCaptcha.getProjectId(), SvnGetFileTypeEnum.all,
                            true,pamsQuoteCaptcha.getProjectGuid());
                    List<SvnEsSearch> esSearches = new ArrayList<>();
                    //首先检查删除更新mongodb数据
                    if(DataUtil.isNotEmpty(svnList)){
                        for(Tree tree :svnList){
                            SvnEsSearch svnEsSearch = new SvnEsSearch();
                            svnEsSearch.setId(tree.getId());
                            svnEsSearch.setProjectGuid(tree.getProjectGuid());
                            svnEsSearch.setFileName(tree.getFileName());
                            svnEsSearch.setPath(tree.getPath());
                            svnEsSearch.setChildren(tree.getChildren());
                            esSearches.add(svnEsSearch);
                            pamsSvnDao.deletePamsSvn(tree.getProjectGuid());
                            esPamsSvnDao.dleteDocument(tree.getProjectGuid(),"es_pams");
                        }
                    }
                    System.out.println("------> " +"开始新增mongodb,es");
                    pamsSvnDao.batchPamsSvn(svnList);
                    if(DataUtil.isNotEmpty(esSearches)){
                        esPamsSvnDao.batchAddDocument(esSearches,"es_pams");
                    }
                    System.out.println("------> " +"结束新增mongodb,es");
                }catch (SVNException svne) {
                    System.err.println("打印版本树时发生错误: "
                            + svne.getMessage());
                    System.exit(1);
                }
            }
        }
    }
}
